﻿@page "/channels"

@attribute [TabItemOption(Text = "通信端口列表")]
@using TrackSystem.Entities.Dtos.Channel;
@using TrackSystem.Entities.Extensions

<PageTitle>通信端口列表</PageTitle>

<div class="page-user">
    <h1>通信端口列表</h1>


    <Table TItem="ChannelWithSensorsDto"
        RenderMode="TableRenderMode.Table"
        class="table-users"
        IsPagination="true"
        PageItemsSource="@PageItemsSource"
        IsFixedHeader="true"
        IsStriped="true"
        IsBordered="true"
        OnQueryAsync="OnQueryAsync">

        <TableColumns>
            <TableColumn @bind-Field="@context.Id" Sortable="true" Width="50" />
            <TableColumn @bind-Field="@context.Name" Sortable="true"  Width="300">
                <Template Context="value">
                    <div class="d-flex">
                        @{
                            var row = (ChannelDto)value.Row;
                        }
                        <div class="ps-2">
                            <div>@value.Value</div>
                            <div class="user-demo-address">@row.Description</div>
                        </div>
                    </div>
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.PortCategory" Align="Alignment.Center">
                <Template Context="value">
                    <div class="ps-2">
                        <div>@value.Value</div>
                        <div>
                            @{
                                var row = (ChannelDto)value.Row;
                                String msg = $"{row.Ip}:{row.Port}";
                                if (row.PortCategory == PortCategory.UDP)
                                    msg = $"远程:{row.Ip}:{row.Port},本地:{row.localIp}:{row.localPort}";
                                else if (row.PortCategory == PortCategory.SRIAL)
                                    msg = $"{row.COMn},{row.Baudrate},{row.SerialParams}";
                            }
                            @msg
                        </div>
                    </div>
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Protocol" Align="Alignment.Center" Width="200">
                <Template Context="value">
                    <div class="ps-2">
                        <div>@value.Value</div>
                        <div>
                        @{
                            var row = (ChannelDto)value.Row;
                            string memo = $"";
                            if (row.Protocol is ProtocolEnum.Modbus or ProtocolEnum.ModbusTCP)
                            {
                                if (row.optSplit3_4Cmd)
                                    memo += "3，4功能分离,";
                                else
                                    memo += "3，4功能合并,";

                                if (row.optSplit3_4Cmd)
                                    memo += "同时读写";
                                else
                                    memo += "读写分离";
                            }
                        }
                        @memo
                        </div>
                    </div>
                </Template>
            </TableColumn>
            <TableColumn @bind-Field="@context.Interval" Align="Alignment.Left" Width="100">
                <Template Context="value">
                    <div>@value.Value ms</div>
                </Template>
            </TableColumn>

            <TableColumn @bind-Field="@context.Sensors" Align="Alignment.Center">
                <Template Context="value">
                    <div class="ps-2">
                        <div>
                            @if (value.Row is { } row)
                            {
                                if (row.Sensors.Count > 0)
                                {
                                    <span class="badge bg-primary">@row.Sensors.Count</span>
                                }
                                else
                                {
                                    <span class="badge bg-secondary">无传感器</span>
                                }
                            }
                        </div>
                        <div>
                            @foreach(var sensor in (value.Row as ChannelWithSensorsDto).Sensors)
                            {
                                <span class="badge bg-secondary">@sensor.Name</span>
                            }
                        </div>
                    </div>
                </Template>
            </TableColumn>
        </TableColumns>
    </Table>
</div>



